home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / XLIB06.ZIP / XLINE.ASM < prev    next >
Assembly Source File  |  1993-09-13  |  5KB  |  317 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XLINE
  3. ;
  4. ; Line drawing functions.
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ;
  14. ; egg@dstos3.dsto.gov.au
  15. ; teg@bart.dsto.gov.au
  16. ;-----------------------------------------------------------------------
  17. include xlib.inc
  18. include xline.inc
  19.  
  20.     .code
  21.  
  22.  
  23. ModeXAddr       macro
  24.     mov    cl,bl
  25.     push    dx
  26.     mov    dx,[_ScrnLogicalByteWidth]
  27.     mul    dx
  28.     pop    dx
  29.     shr    bx,2
  30.     add    bx,ax
  31.     add     bx,[PgOffs]
  32.     and    cl,3
  33.     endm
  34.  
  35.  
  36. ;-----------------------------------------------------------------------
  37. ; _x_line
  38. ;
  39. ; Line drawing function for all MODE X 256 Color resolutions
  40. ; Based on code from "PC and PS/2 Video Systems" by Richard Wilton.
  41. ;
  42. ; Compile with Tasm.
  43. ; C callable.
  44. ;
  45.  
  46. _x_line     proc
  47. ARG     x1:word,y1:word,x2:word,y2:word,Color:word,PgOffs:word
  48. LOCAL   vertincr:word,incr1:word,incr2:word,routine:word=LocalStk
  49.     push    bp        ; Set up stack frame
  50.     mov    bp,sp
  51.     sub    sp,LocalStk
  52.     push    si
  53.     push    di
  54.  
  55.     mov    ax,0a000h
  56.     mov    es,ax
  57.  
  58.     mov    dx,SC_INDEX    ; setup for plane mask access
  59.  
  60. ; check for vertical line
  61.  
  62.     mov    si,[_ScrnLogicalByteWidth]
  63.     mov    cx,[x2]
  64.     sub    cx,[x1]
  65.     jz    VertLine
  66.  
  67. ; force x1 < x2
  68.  
  69.     jns    L01
  70.  
  71.     neg    cx
  72.  
  73.     mov    bx,[x2]
  74.     xchg    bx,[x1]
  75.     mov    [x2],bx
  76.  
  77.     mov    bx,[y2]
  78.     xchg    bx,[y1]
  79.     mov    [y2],bx
  80.  
  81. ; calc dy = abs(y2 - y1)
  82.  
  83. L01:
  84.     mov    bx,[y2]
  85.     sub    bx,[y1]
  86.     jnz    short skip
  87.     jmp     HorizLine
  88. skip:        jns    L03
  89.  
  90.     neg    bx
  91.     neg    si
  92.  
  93. ; select appropriate routine for slope of line
  94.  
  95. L03:
  96.     mov    [vertincr],si
  97.     mov    [routine],offset LoSlopeLine
  98.     cmp    bx,cx
  99.     jle    L04
  100.     mov    [routine],offset HiSlopeLine
  101.     xchg    bx,cx
  102.  
  103. ; calc initial decision variable and increments
  104.  
  105. L04:
  106.     shl    bx,1
  107.     mov    [incr1],bx
  108.     sub    bx,cx
  109.     mov    si,bx
  110.     sub    bx,cx
  111.     mov    [incr2],bx
  112.  
  113. ; calc first pixel address
  114.  
  115.     push    cx
  116.     mov    ax,[y1]
  117.     mov    bx,[x1]
  118.     ModeXAddr
  119.     mov    di,bx
  120.     mov    al,1
  121.     shl    al,cl
  122.     mov    ah,al        ; duplicate nybble
  123.     shl    al,4
  124.     add    ah,al
  125.     mov    bl,ah
  126.     pop    cx
  127.     inc    cx
  128.     jmp    [routine]
  129.  
  130. ; routine for verticle lines
  131.  
  132. VertLine:
  133.     mov    ax,[y1]
  134.     mov    bx,[y2]
  135.     mov    cx,bx
  136.     sub    cx,ax
  137.     jge    L31
  138.     neg    cx
  139.     mov    ax,bx
  140.  
  141. L31:
  142.     inc    cx
  143.     mov    bx,[x1]
  144.     push    cx
  145.     ModeXAddr
  146.  
  147.     mov    ah,1
  148.     shl    ah,cl
  149.     mov    al,MAP_MASK
  150.     out    dx,ax
  151.     pop    cx
  152.     mov    ax, word ptr [Color]
  153.  
  154. ; draw the line
  155.  
  156. L32:
  157.     mov    es:[bx],al
  158.     add    bx,si
  159.     loop    L32
  160.     jmp    Lexit
  161.  
  162. ; routine for horizontal line
  163.  
  164. HorizLine:
  165.     push    ds
  166.  
  167.     mov    ax,[y1]
  168.     mov    bx,[x1]
  169.     ModeXAddr
  170.  
  171.     mov    di,bx     ; set dl = first byte mask
  172.     mov    dl,00fh
  173.     shl    dl,cl
  174.  
  175.     mov    cx,[x2] ; set dh = last byte mask
  176.     and    cl,3
  177.     mov    dh,00eh
  178.     shl    dh,cl
  179.     not    dh
  180.  
  181. ; determine byte offset of first and last pixel in line
  182.  
  183.     mov    ax,[x2]
  184.     mov    bx,[x1]
  185.  
  186.     shr    ax,2     ; set ax = last byte column
  187.     shr    bx,2    ; set bx = first byte column
  188.     mov    cx,ax    ; cx = ax - bx
  189.     sub    cx,bx
  190.  
  191.     mov    ax,dx    ; mov end byte masks to ax
  192.     mov    dx,SC_INDEX ; setup dx for VGA outs
  193.     mov     bx, [Color]
  194.  
  195. ; set pixels in leftmost byte of line
  196.  
  197.     or    cx,cx      ; is start and end pt in same byte
  198.     jnz    L42        ; no !
  199.     and    ah,al      ; combine start and end masks
  200.     jmp    short L44
  201.  
  202. L42:            push    ax
  203.     mov     ah,al
  204.     mov     al,MAP_MASK
  205.     out     dx,ax
  206.     mov    al,bl
  207.     stosb
  208.     dec    cx
  209.  
  210. ; draw remainder of the line
  211.  
  212. L43:
  213.     mov    ah,0Fh
  214.     mov    al,MAP_MASK
  215.     out    dx,ax
  216.     mov    al,bl
  217.     rep    stosb
  218.     pop     ax
  219.  
  220. ; set pixels in rightmost byte of line
  221.  
  222. L44:
  223.     mov    al,MAP_MASK
  224.     out    dx, ax
  225.     mov     byte ptr es:[di],bl
  226.     pop    ds
  227.     jmp    short Lexit
  228.  
  229.  
  230. ; routine for dy >= dx (slope <= 1)
  231.  
  232. LoSlopeLine:
  233.     mov    al,MAP_MASK
  234.     mov    bh,byte ptr [Color]
  235. L10:
  236.     mov    ah,bl
  237.  
  238. L11:
  239.     or    ah,bl
  240.     rol    bl,1
  241.     jc    L14
  242.  
  243. ; bit mask not shifted out
  244.  
  245.     or    si,si
  246.     jns    L12
  247.     add    si,[incr1]
  248.     loop    L11
  249.  
  250.     out    dx,ax
  251.     mov    es:[di],bh
  252.     jmp    short Lexit
  253.  
  254. L12:
  255.     add    si,[incr2]
  256.     out    dx,ax
  257.     mov    es:[di],bh
  258.     add    di,[vertincr]
  259.     loop    L10
  260.     jmp    short Lexit
  261.  
  262. ; bit mask shifted out
  263.  
  264. L14:            out    dx,ax
  265.     mov    es:[di],bh
  266.     inc    di
  267.     or    si,si
  268.     jns    L15
  269.     add    si,[incr1]
  270.     loop    L10
  271.     jmp    short Lexit
  272.  
  273. L15:
  274.     add    si,[incr2]
  275.     add    di,[vertincr]
  276.     loop    L10
  277.     jmp    short Lexit
  278.  
  279. ; routine for dy > dx (slope > 1)
  280.  
  281. HiSlopeLine:
  282.     mov    bx,[vertincr]
  283.     mov    al,MAP_MASK
  284. L21:            out    dx,ax
  285.     push    ax
  286.     mov    ax,[Color]
  287.     mov    es:[di],al
  288.     pop    ax
  289.     add    di,bx
  290.  
  291. L22:
  292.     or    si,si
  293.     jns    L23
  294.  
  295.     add    si,[incr1]
  296.     loop    L21
  297.     jmp    short Lexit
  298.  
  299. L23:
  300.     add    si,[incr2]
  301.     rol    ah,1
  302.     adc    di,0
  303. lx21:    loop    L21
  304.  
  305. ; return to caller
  306.  
  307. Lexit:
  308.     pop    di
  309.     pop    si
  310.     mov    sp,bp
  311.     pop    bp
  312.     ret
  313.  
  314. _x_line    endp
  315.  
  316. end
  317.